package edu.cmich.cps.cps240.partyprincesspalace.hw06;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;

/**
 * Toolbox of chips that we can add to the workspace
 * @author holde1ja
 */
public class Toolbox extends JPanel {
	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unused") //the chip buttons receive GUI events, 
								//thus they actually ARE being used
	private Vector<ChipButton> chips;  //  @jve:decl-index=0:
	private Workspace workspace;

	/**
	 * This is the default constructor
	 * @param p_workspace a handle to the workspace so the
	 * buttons we construct can know who to talk to.
	 */
	public Toolbox(Workspace p_workspace) {
		super();
		
		workspace = p_workspace;
		
		initialize();
	}

	/**
	 * A basic template for our predefined chips.  These are for
	 * chips with 2 inputs (named "a" and "b") and 1 output (named "out")
	 * @param name The name of the type of chip this button creates
	 * @return The chip "template" to build copies from
	 */
	private Chip generate2to1Chip(String name)
	{
		Vector<ChipPin> inputs = new Vector<ChipPin>();
		Vector<ChipPin> outputs = new Vector<ChipPin>();
		
		ChipPin a = new ChipPin("a", null, null);
		ChipPin b = new ChipPin("b", null, null);
		ChipPin out = new ChipPin("out", null, null);
		
		Rectangle r = new Rectangle(0,0,40,30);
		
		inputs.add(a); inputs.add(b);
		outputs.add(out);
		
		Chip buildChip = new Chip(r, inputs, outputs, name);
		
		return buildChip;
	}
	
	/**
	 * This method initializes the toolbox's predefined chips
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLayout(new GridBagLayout());
		chips = new Vector<ChipButton> ();
		ButtonGroup grp = new ButtonGroup();
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		
		//Add Wire tool
		WireButton wire = new WireButton(workspace);
		c.gridx = 0; c.gridy = 0;
		this.add(wire, c);
		grp.add(wire);
		
		//EMPTY SPACE AT x=1 Y=0 FOR FUTURE TOOL (grabbing tool)
		
		//Add InputBus
		InputBus in = new InputBus(new Rectangle(0,0,40,30));
		in.setName("in");
		ChipButton button = new ChipButton(in, workspace);
		c.gridx = 0; c.gridy = 1;
		this.add(button, c);
		grp.add(button);
		
		//Add OutputBus
		OutputBus out = new OutputBus(new Rectangle(0,0,40,30));
		out.setName("out");
		button = new ChipButton(out, workspace);
		c.gridx = 1; c.gridy = 1;
		this.add(button, c);
		grp.add(button);
		
		//Create AND gate
		button = 
			new ChipButton(generate2to1Chip("And"), workspace);
		c.gridx = 0; c.gridy = 2;
		this.add(button,c);
		grp.add(button);
		
		//Create a NAND gate
		button = 
			new ChipButton(generate2to1Chip("Nand"), workspace);
		c.gridx = 1; c.gridy = 2;
		this.add(button,c);
		grp.add(button);
		
		//Create an OR gat
		button = 
			new ChipButton(generate2to1Chip("Or"), workspace);
		c.gridx = 0; c.gridy = 3;
		this.add(button,c);
		grp.add(button);
		
		//Create a NOR gate
		button = 
			new ChipButton(generate2to1Chip("Nor"), workspace);
		c.gridx = 1; c.gridy = 3;
		this.add(button,c);
		grp.add(button);
		
		//Create an XOR gate
		button = 
			new ChipButton(generate2to1Chip("Xor"), workspace);
		c.gridx = 0; c.gridy = 4;
		this.add(button,c);
		grp.add(button);
		//Create an XNOR gate
		button = 
			new ChipButton(generate2to1Chip("Xnor"), workspace);
		c.gridx = 1; c.gridy = 4;
		this.add(button,c);
		grp.add(button);
	}

}
